package com.gisuni.sddc.platformManager.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import com.gisuni.sddc.platformManager.dao.ServerResourceDao;
import com.gisuni.sddc.platformManager.model.LayerInfo;
import com.gisuni.sddc.platformManager.model.ModuleInfo;
import com.gisuni.sddc.platformManager.model.ModuleRLayer;
import com.gisuni.sddc.platformManager.model.Parameter;
import com.gisuni.sddc.platformManager.model.ServerInfo;
import com.gisuni.sddc.platformManager.pageModel.Tree;
import com.gisuni.sddc.service.LoginService;

@IocBean
public class ServerResourceService {
//	private Log log = Logs.getLog(ServerResourceService.class);
	private  Log log = Logs.getLog(ServerResourceService.class);
	@Inject
	private ServerResourceDao serverResourceDao;

	
	public List<Tree> allTreeNode() {
		List<ServerInfo> Serverlist = serverResourceDao.getAllServerTreeNode();
		List<LayerInfo> Layerlist = serverResourceDao.getAllLayerTreeNode();

		List<Tree> listTrees = new ArrayList<Tree>();
		
		//服务List
		if(Serverlist!=null && Serverlist.size()>0){
			for (ServerInfo ServerInfo : Serverlist) {
				Tree tree = new Tree();
				tree.setId(ServerInfo.getId());
				tree.setText(ServerInfo.getServerName());
				tree.setPid("0"); 
				listTrees.add(tree);
			}
		}else{
			return null;
		}

		
		//图层List
		for (LayerInfo LayerInfo : Layerlist) {
			
			String serverId = LayerInfo.getServerId();
			boolean flag = false;
			for (ServerInfo serverInfo : Serverlist) {
				if(serverInfo.getId().equals(serverId)){
					flag = true ; 
					break;
				}
			}
			//过滤掉不可用服务下对应的图层
			if(flag){
				Tree tree = new Tree();
				tree.setId(LayerInfo.getId());
				tree.setText(LayerInfo.getLayerName());
				tree.setPid(LayerInfo.getServerId());
				listTrees.add(tree);
			}

		}
		
        Tree root = new Tree();
        root.setId("0");
        root.setText("可操作图层");
        listTrees.add(root);
		return listTrees;
	}

	public List<Tree> allModelTreeNode() throws Exception{
		List<Parameter> parameters = serverResourceDao.getAllParameters();
		Map<String, Parameter> pamap = new HashMap<>();
		for (Parameter para : parameters) {
			pamap.put(para.getId(), para);
		}
		List<ModuleInfo> infos = serverResourceDao.getAllModuleTreeNode();
		List<Tree> listTrees = new ArrayList<Tree>();
		for (ModuleInfo module : infos) {
			Tree tree = new Tree();
			tree.setId(module.getId());
			tree.setText(module.getModelName());
			tree.setPid(module.getParentId());
			listTrees.add(tree);
			String paras = module.getParameters();
			if (paras != null) {
				for (String p : paras.split(",")) {
					Tree tree1 = new Tree();
					if(pamap.get(p)!=null){
						tree1.setId(pamap.get(p).getId());
						tree1.setText(pamap.get(p).getPname());
						tree1.setPid(module.getId());
						listTrees.add(tree1);
					}
				}
			}
		}
		return listTrees;
	}

	public List<ModuleRLayer> getConfFieldsByid(String id, String pid) {
		return serverResourceDao.getConfFieldsByid(id,pid);
		
	}

	public Object getLayerInfosByIds(String serverId, String layerId) {
		return serverResourceDao.getLayerInfosByIds(serverId,layerId);
	}

	public boolean insertOrUpdateActionServerConfig(String moduleId, String layerId, String fieldsExpress, String layerParam) {
		return serverResourceDao.insertOrActionServerConfig(moduleId,layerId,fieldsExpress,layerParam);
	}

	public ModuleRLayer getModuleServerConfigByMAL(String moduleId, String layerParam) {
		
		return serverResourceDao.getModuleServerConfigByMAL(moduleId,layerParam);
	}

}
